package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.CustomException;
import com.example.dto.SetmealDto;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.Setmeal;
import com.example.entity.SetmealDish;
import com.example.service.CategoryService;
import com.example.service.DishService;
import com.example.service.SetmealDishService;
import com.example.service.SetmealService;
import com.example.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.management.ClassLoadingMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author bd
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;
    @GetMapping("/page")
    public R<Page> list(int page, int pageSize,String name)
    {
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto> pageInfoDto=new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> LambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper.eq(!StringUtils.isEmpty(name),Setmeal::getName,name);
        LambdaQueryWrapper.orderByDesc(Setmeal::getCreateTime);
        setmealService.page(pageInfo,LambdaQueryWrapper);

        BeanUtils.copyProperties(pageInfo,pageInfoDto,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> collect = records.stream().map((item) -> {
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(item,dto);
            Category category = categoryService.getById(item.getCategoryId());
            dto.setCategoryName(category.getName());
            return dto;
        }).collect(Collectors.toList());

        pageInfoDto.setRecords(collect);
        return R.success(pageInfoDto);
    }



    @PostMapping
    @Transactional
    public R<String> add(@RequestBody SetmealDto setmealDto)
    {
        //
        setmealService.save(setmealDto);
        //还要向setmeal_dish表写入具体的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect=new ArrayList<>();
        collect = setmealDishes.stream().map((item) -> {
            SetmealDish setmealDish = new SetmealDish();
            BeanUtils.copyProperties(item,setmealDish);
            setmealDish.setSetmealId(setmealDto.getCategoryId().toString());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
        return R.success("添加成功");
    }

    //批量删除

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids)
    {
         //第一步删除setmeal 首先判断改套餐是否在销售 status=1
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = setmealService.count(wrapper);
        if(count>0){
           // throw new CustomException("该套餐正在销售");

            //systemctl start network.service
            return R.error("该套餐正在销售");
        }
        //否者可以删除
        setmealService.removeByIds(ids);//一次删除多条
        //第二步删除setmealdish
        LambdaQueryWrapper<SetmealDish> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(wrapper1);
         return R.success("删除成功");
    }

    //修改状态
    @PostMapping("/status/{status}")
    public R<String> change(@PathVariable int status,@RequestParam List<Long> ids)
    {
        LambdaUpdateWrapper<Setmeal> wrapper=new LambdaUpdateWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.set(Setmeal::getStatus,status);
        setmealService.update(wrapper);
//        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
//        wrapper.in(Setmeal::getId,ids);
//        wrapper.
//
//        Setmeal byId = setmealService.getById(ids);
//        byId.setStatus(status);
//        setmealService.updateById(byId);
        return R.success("修改成功！");
    }
}
